#pragma once 

#include <cstdio>
#include <cstdarg>
#include <ctime>
#include <string>

#define LOGFILE "./logFile.txt"

// 日志等级
#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define FATAL 4

#define BUFFERSIZE 1024

const char *gLevel[] = {
    "DEBUG",
    "NORMAL",
    "WANING",
    "ERROR",
    "FATAL"};

void logMessage(int level, const char* format, ...)
{
    char stdBuffer[BUFFERSIZE];
    time_t timetamp = time(nullptr);

    struct tm * time1 = localtime(&timetamp);
    int year = time1->tm_year;
    int month = time1->tm_mon;
    int day = time1->tm_mday;
    int hour = time1->tm_hour;
    int minutes = time1->tm_min;

    snprintf(stdBuffer, sizeof stdBuffer, "[%d/%d/%d] %s: %s", year, month, day, gLevel[level], stdBuffer);

    va_list args;
    va_start(args, format);
    char logBuffer[BUFFERSIZE];

    vsnprintf(logBuffer, sizeof logBuffer, format, args);
    va_end(args);

    FILE *fp = fopen(LOGFILE, "a");
    fprintf(fp,"%s%s\n", stdBuffer, logBuffer);
    fclose(fp);
}

static void LogPrint(int level, std::string args)
{
    printf("[%s] file:%s line:%d\n %s",gLevel[level],__FILE__, __LINE__,args.c_str());
}
